package com.xt.service.impl;

import com.xt.bean.Specialty;
import com.xt.dao.SpecialtyRepository;
import com.xt.service.SpecialtyService;
import com.xt.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * Created by XT on 2018/5/4.
 */
@Service
public class SpecialtyServiceImpl implements SpecialtyService {

    @Autowired
    private SpecialtyRepository sr;

    @Override
    public void saveOrUpdateOne(Specialty c) {

        if (StringUtils.isEmpty(c.getId())) {
            //新增
            c.setCreateDate(new Date());
        }

        c.setUpdateDate(new Date());
        sr.save(c);
    }

    @Override
    public void deleteOne(String id) {
        //删除关联关系

        sr.delete(id);
    }

    @Override
    public Specialty getById(String id) {
        return sr.findOne(id);
    }

    @Override
    public Page<Specialty> getListByPage(int count, int pageNo, String sortDirection, String... sortField) {
        Page<Specialty> specialtysPage = sr.findAll(PageUtil.getPageable(count, pageNo, sortDirection, sortField));
        return specialtysPage;
    }

    @Override
    public Page<Specialty> getPageByNameLikeAndDepartmentId(String departmentId, String name, int count, int pageNo, String sortDirection, String... sortField) {
        Page<Specialty> specialtyCoursesPage = sr.findByDepartmentIdAndNameLike(departmentId, name, PageUtil.getPageable(count, pageNo, sortDirection, sortField));
        return specialtyCoursesPage;
    }

    @Override
    public Page<Specialty> getPageByNameLike(String name, int count, int pageNo, String sortDirection, String... sortField) {
        Page<Specialty> specialtyCoursesPage = sr.findByNameLike(name, PageUtil.getPageable(count, pageNo, sortDirection, sortField));
        return specialtyCoursesPage;
    }

    @Override
    public Page<Specialty> getPageByDepartmentId(String departmentId, int count, int pageNo, String sortDirection, String... sortField) {
        Page<Specialty> specialtyCoursesPage = sr.findByDepartmentId(departmentId, PageUtil.getPageable(count, pageNo, sortDirection, sortField));
        return specialtyCoursesPage;
    }

}
